package com.example.bakend.controller;

import com.example.bakend.pojo.Book;
import com.example.bakend.pojo.Record;
import com.example.bakend.pojo.Result;
import com.example.bakend.service.BookService;
import com.example.bakend.service.ReaderService;
import com.example.bakend.service.RecordService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("record")
public class RecordController {
    @Autowired
    RecordService recordService;
    @Autowired
    BookService bookService;
    @Autowired
    ReaderService readerService;

    @PostMapping
    public Result insertRecord(@RequestBody Record record) {
        if (record.getBorrowDate().after(record.getReturnDate()) || (record.getBorrowDate().before(new Date()))) {
            return Result.error("时间错误");
        }
        if (readerService.selectReader(record.getReaderId()) == null){
            return Result.error("读者不存在");
        }
//        判断书是否还有
        Book book = bookService.selectBook(record.getBookId());
        if (book == null){
            return Result.error("没有相关书籍");
        }else if (book.getStockQuantity() < record.getNum()){
            return Result.error("库存不足");
        }else if (record.getNum() >= 4){
            return Result.error("借的数量过多");
        }
//        借书，修改库存数量
        book.setStockQuantity(book.getStockQuantity() - record.getNum());
        bookService.updateBook(book);

        return Result.success(recordService.insertRecord(record));
    }

    @GetMapping("{id}")
    public Result selectRecord(@PathVariable Integer id) {
        return Result.success(recordService.selectRecord(id));
    }

//    @GetMapping
//    public Result selectRecords(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
//                                @RequestParam(name = "pageSize", defaultValue = "2") Integer pageSize) {
//        return Result.success(recordService.selectRecords(pageNum, pageSize));
//    }

//    作用：展示列表数据、 条件查询
    @GetMapping
    public Result getRecords(@RequestParam(name = "readerId", required = false) Integer readerId,
                             @RequestParam(name = "bookId", required = false) Integer bookId,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime borrowDateBegin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime borrowDateEnd,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(name = "pageSize", defaultValue = "2") Integer pageSize) {

        return Result.success(recordService.getRecords(readerId, bookId, borrowDateBegin, borrowDateEnd, pageNum, pageSize));
    }

    @DeleteMapping("{ids}")
    public Result deleteRecord(@PathVariable List<Integer> ids) {
        return Result.success(recordService.deleteRecord(ids));
    }

//    根据bookid查询record表格
    @GetMapping("bookId")
    public Result selectBookIdRecord(Integer BookId){
        return Result.success(recordService.selectBookIdRecord(BookId));
    }

//    归还book
    @PutMapping("{id}")
    public Result getRestore(@PathVariable Integer id){
//        获取当前列的bookid
        Integer bookId = recordService.selectRecord(id).getBookId();

//        获取当前列的book的现有库存
        Book book = bookService.selectBook(bookId);
        Integer stockQuantity = book.getStockQuantity();

//        获取借了多少书
        Integer num = recordService.selectRecord(id).getNum();

        //还书，修改库存数量  这个还没赋值到数据库
        book.setStockQuantity(stockQuantity + num);

//        修改数据库
        bookService.updateBook(book);

        return Result.success(recordService.getRestore(id));
    }


}
